using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Signup : MinimalPageBase
{
    new protected void Page_Load(object sender, EventArgs e)
    {

    }   

    protected void CreateNewAccountButton_Click(object sender, EventArgs e)
    {
        bool succeeded = false;
        try
        {
            string userName = UserNameTextBox.Text.Trim();
            string password = PasswordTextBox.Text.Trim();
            string email = EmailTextBox.Text.Trim();
            if (userName.Length > 12 || password.Length > 12 || email.Length > 40) throw new Exception("Invalid length of input");
            MembershipUser user = Membership.CreateUser(userName, password);
            if (user != null) FormsAuthentication.SetAuthCookie(user.UserName, true);
            succeeded = true;
        }
        catch (MembershipCreateUserException userCreationException)
        {
            errorLabel.Visible = true;
            switch (userCreationException.StatusCode)
            {
                case MembershipCreateStatus.Success:
                    Server.Transfer("Default.aspx");
                    break;

                case MembershipCreateStatus.DuplicateUserName:
                    errorLabel.Text = "Username already exists. Please enter a different user name.";
                    break;

                case MembershipCreateStatus.DuplicateEmail:
                    errorLabel.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
                    break;

                case MembershipCreateStatus.InvalidPassword:
                    errorLabel.Text = "The password provided is invalid. Please enter a valid password value.";
                    break;

                case MembershipCreateStatus.InvalidEmail:
                    errorLabel.Text = "The e-mail address provided is invalid. Please check the value and try again.";
                    break;                                   

                case MembershipCreateStatus.InvalidUserName:
                    errorLabel.Text = "The user name provided is invalid. Please check the value and try again.";
                    break;

                case MembershipCreateStatus.ProviderError:
                    errorLabel.Text = "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                    break;

                case MembershipCreateStatus.UserRejected:
                    errorLabel.Text = "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                    break;

                default:
                    errorLabel.Text = "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        if (succeeded)
        {
            Server.Transfer("Default.aspx");
        }
    }
}
